package com.burgeon.backend.Rest.User;
/**
 * 获取门店导购
 */


import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.Employee;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindEmpByStoreId extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindEmpByStoreId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<Employee> getData(String id) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(id), url);
        System.err.println(str);

        //String count = new JSONArray(str).getJSONObject(0).getString("count");

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Employee> data = new ArrayList<>();
        for (int i =0;i<jsonArray.length();i++){
            JSONArray jsonArray1 = jsonArray.getJSONArray(i);
            Employee employee = new Employee();
            employee.setId(jsonArray1.getString(0));
            employee.setName(jsonArray1.getString(1));
            employee.setSex(jsonArray1.getString(2));
            employee.setPhone(jsonArray1.getString(3));
            employee.setNo(jsonArray1.getString(4));
            data.add(employee);
        }


        return data;
    }

    private MultiValueMap Data(String sid) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("ID");

        columns.add("NAME");//\
        columns.add("SEX");//\
        columns.add("PHONE");//\
        columns.add("NO");


        params.put("columns", columns);
        params.put("table", "C_V_EMPLOYEE");//表名

        params.put("count", "true");

        HashMap orderby = new HashMap();

        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();

        expr1.put("combine", "and");

        expr1v1.put("column", "'ISSALER'");//营业员
        expr1v1.put("condition", "'Y'");
        expr1.put("expr1", expr1v1);

        expr1v2.put("column", "'ISACTIVE'");//可用
        expr1v2.put("condition", "'Y'");
        expr1.put("expr2", expr1v2);

        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();

        expr2.put("combine", "and");

        expr2v1.put("column", "'C_STORE_ID'");//
        expr2v1.put("condition", "'"+sid+"'");
        expr2.put("expr1", expr2v1);

        expr2v2.put("column", "'INCUMBENCY_STS'");//
        expr2v2.put("condition", "'I'");
        expr2.put("expr2", expr2v2);

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);


        params.put("params", paramss);
        return DATA("Query", params, account, password);
    }


    public static void main(String[] args) {
        FindEmpByStoreId g = new FindEmpByStoreId("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("3391"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
